package com.tee.service.privilege.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tee.bean.privilege.Department;
import com.tee.service.base.DaoSupport;
import com.tee.service.privilege.DepartmentService;

@Service
public class DepartmentServiceBean extends DaoSupport<Department> implements DepartmentService{

	public Department findById(Integer departmentid) {
		return (Department) em.createQuery("select o from Department o where o.id=?1")
		.setParameter(1, departmentid).getSingleResult();
	}
	
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public boolean exsit(String name) {
		long count = (Long) em.createQuery(
				"select count(o) from Department o where o.name=?1").setParameter(1, name).getSingleResult();
		return count > 0;
	}

	public boolean deleteById(Integer id) {
		Integer result = em.createQuery("delete from Department o where o.id=?1").setParameter(1, id).executeUpdate();
		return result>0;
	}

	public Department findByName(String name) {
		return (Department) em.createQuery("select o from Department o where o.name=?1")
		.setParameter(1, name).getSingleResult();
	}

}
